<?php

namespace app\controller\api;

use app\service\AliyunService;
use app\service\UserDeviceBindingService;
use app\validate\UserDeviceBindingValidate;
use Exception;
use share\controller\ApiBaseController;
use share\service\AmqpService;
use share\service\ContextService;
use share\service\MQService;
use think\Log;

class UserDeviceBindingController extends ApiBaseController{

    protected $beforeActionList = [
        "checkSign" => ['except' => ['unbind','unbind_v2']],
        "checkUser" => ['except' => ['unbind','unbind_v2','is_bind','is_bind_simple']], 
        'checkAuthorize' => ['only' => ['unbind','unbind_v2']],
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){        
        $this->validate = new UserDeviceBindingValidate();
    }

    /**
     * 绑定
     */
    public function bind_action(){
        $input = $this->check();
        foreach($input['info'] as $info){
            $this->check('infoDetail',$info);
        }
        $vendor = ContextService::getVendor();
        $userInfo = ContextService::getUserInfo();
        $res = UserDeviceBindingService::userBind($input,$vendor,$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 删除绑定
     */
    public function delete_action(){
        $input = $this->check(); 
        $userInfo = ContextService::getUserInfo();      
        $res = UserDeviceBindingService::deleteBindDevice($input,$userInfo['id']);
        return self::ok($res);
    }


    /**
     * 修改绑定信息
     */
    public function change_bind_info_action(){
        $input = $this->check();
        foreach($input['info'] as $info){
            $this->check('infoDetail',$info);
        }
        $res = UserDeviceBindingService::changeBindInfo($input,ContextService::getVendor());
        return self::ok($res);
    }

    /**
     * 获取绑定者信息
     */
    public function get_user_action(){
        $input = $this->check();
        $userInfo = ContextService::getUserInfo();
        $vendor = ContextService::getVendor();
        $res = UserDeviceBindingService::getBindUserInfo($input,$vendor,$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 获取绑定者信息 (感觉原接口写的有问题)
     */
    public function get_user_v2_action(){
        $input = $this->check();
        $userInfo = ContextService::getUserInfo();
        $vendor = ContextService::getVendor();
        $res = UserDeviceBindingService::getBindUserInfo($input,$vendor,$userInfo['id']);
        return self::ok($res);
    }

    /**
     * 设备是否被绑定
     */
    public function is_bind_action(){
        $input = $this->check();
        $res = UserDeviceBindingService::isBind($input,ContextService::getVendor());
        return self::ok($res);
    }

    /**
     * 设备是否被绑定
     */
    public function is_bind_simple_action(){
        $input = $this->check();
        $res = UserDeviceBindingService::isBindSimple($input,ContextService::getVendor());
        return self::ok($res);
    }

    
    /**
     * 设备解绑
     */
    public function unbind_action(){
        $input = $this->check();
        $res = UserDeviceBindingService::unbind($input);

        AmqpService::simplePush(
            [
                'service' => 'app\\service\\AliyunProxyService@unbindAllUser',
                'params' => [
                    'productKey' => $input["productKey"],
                    'deviceName' => $input["deviceName"],
                    'deviceType' => $input["deviceType"],
                    "vendor" => ContextService::getVendor(),
                ],
            ]
        );

        // MQService::push(
        //     [
        //         'service' => 'app\\service\\AliyunProxyService@unbindAllUser',
        //         'params' => [
        //             'productKey' => $input["productKey"],
        //             'deviceName' => $input["deviceName"],
        //             'deviceType' => $input["deviceType"],
        //             "vendor" => ContextService::getVendor(),
        //         ],
        //     ],MQService::CHANNEL_OF_ACCOUNT);
        

        // try{
        //     AliyunService::rawUnbindDevice($input);
        // }
        // catch(Exception $e){
        //     trace("unbind aliyun device fail productKey:".$input['productKey']." deviceName:".$input['deviceName'],Log::ERROR);
        //     trace($e->getTraceAsString(),Log::ERROR);
        // };
        return self::ok($res);
    }

    /**
     * 设备解绑
     */
    public function unbind_v2_action(){
        $input = $this->check("unbind");
        AmqpService::simplePush(
            [
                'service' => 'app\\service\\AliyunProxyService@unbindAllUser',
                'params' => [
                    'productKey' => $input["productKey"],
                    'deviceName' => $input["deviceName"],
                    'deviceType' => $input["deviceType"],
                    "vendor" => ContextService::getVendor(),
                ],
            ]
        );

        // MQService::push(
        //     [
        //         'service' => 'app\\service\\AliyunProxyService@unbindAllUser',
        //         'params' => [
        //             'productKey' => $input["productKey"],
        //             'deviceName' => $input["deviceName"],
        //             'deviceType' => $input["deviceType"],
        //             "vendor" => ContextService::getVendor(),
        //         ],
        //     ],MQService::CHANNEL_OF_ACCOUNT);
        $res = UserDeviceBindingService::unbind($input);
        return self::ok($res);
    }
}